package com.zpark.controller;

import com.alibaba.fastjson.JSON;
import com.zpark.common.Content;
import com.zpark.common.Path;
import com.zpark.common.R;
import com.zpark.service.AdminService;
import com.zpark.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/admin/*")
public class AdminController extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String pathInfo = req.getPathInfo();
        String methodName = pathInfo.substring(1); // 去掉前导斜杠
        try {
            // 反射的方式：获取当前类的 - methodName的方法
            Method[] methods = this.getClass().getMethods();
            for (Method method : methods) {
                // method 每一个方法
                Path annotation = method.getAnnotation(Path.class);
                if (annotation != null) {
                    String value = annotation.value();
                    if (value.equals(methodName) || value.equals(pathInfo)) {
                        method.invoke(this, req, resp);
                        return;
                    }
                } else {
                    resp.getWriter().write("no path");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write(JSON.toJSONString(R.error("访问失败")));
        }
    }

    @Path("/login")
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (adminService.login(req.getParameter("username"), req.getParameter("password"))) {
            // 登录成功
            // 记录用户的会话，Session
            HttpSession session = req.getSession();
            session.setAttribute(Content.USER_SESSION_KEY, req.getParameter("username"));
            resp.getWriter().println(JSON.toJSONString(R.ok("用户登录成功")));
            return;
        } else {
            resp.getWriter().println(JSON.toJSONString(R.error("用户名或密码错误")));
        }
    }

    // 登出系统，确认项目的 会话技术选型
    @Path("/logout")
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (adminService.logout(req.getSession())) {
            resp.getWriter().println(JSON.toJSONString(R.ok("用户退出成功")));
        } else {
            resp.getWriter().println(JSON.toJSONString(R.error("用户退出错误")));
        }
    }

    @Path("/edit-password")
    public void editPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (adminService.editPassword(Integer.parseInt(req.getParameter("id")), req.getParameter("oldPassword"), req.getParameter("newPassword"))) {
            resp.getWriter().println(JSON.toJSONString(R.ok("用户修改密码成功")));
        } else {
            resp.getWriter().println(JSON.toJSONString(R.error("用户修改密码失败")));
        }
    }
}



